﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RegisterationCommon.Student;
using System.Data;
using RegisterationCommon.Common;
using RegisterationCommon.School;
using RegisterationCommon.Education;
using System.IO;

namespace RegisterationSystem.Space_School
{
    public partial class students_unaudited : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Form["action"] != "")
                {
                    switch (Request.Form["action"])
                    {
                        case "updstatusList":
                            UpdStatusList();
                            break;
                        case "updstatusbyid":
                            UpdStatusByID();
                            break;
                        case "updstatusgiveupList":
                            UpdStatusGiveUpList();
                            break;
                        case "updstatusgiveupid":
                            UpdStatusByGiveUpID();
                            break;
                        case "delstudentlist":
                            DelStudentList();
                            break;
                        case "delstudentByID":
                            DelStudentByID();
                            break;
                        case "pswdreset":
                            UpdStudentPswdReset();
                            break;
                        //case "derive_excelList":
                        //    derive_excelList();
                        //    break;
                        //case "derive_excelListAll":
                        //    derive_excelListAll();
                        //    break;
                    }
                }
                Bind();
            }
        }
        //绑定学校
        void Bind()
        {
            //DataTable dt = DBAction.GetDTFromSQL("select * from SchoolInfo where id =" + SchoolAccount.Current.SchID + "");
            DataTable dt = DBAction.GetDTFromSQL("select * from SchoolInfo where id =" + Cookies.Get("SchID") + "");
            if (dt.Rows.Count > 0)
            {
                ListItem item = new ListItem();
                item.Text += dt.Rows[0]["Name"].ToString();
                item.Value = dt.Rows[0]["id"].ToString();
                ddlSchoolInfo.Items.Add(item);
            }
        }

        //根据ID删除
        void DelStudentByID()
        {
            string id = Request.Form["id"];
            string deletereason = Request.Form["deletereason"];
            StudentInfo s = new StudentInfo();
            s.ID = Convert.ToInt32(id);
            s.IsDelete = true;
            s.DeleteReason = deletereason;
            s.DeleteorGiveupStatic();

        }
        //批量删除
        void DelStudentList()
        {
            //获删除改ID
            string idlist = Request.Form["id"];
            string deletereason = Request.Form["deletereason"];
            string[] ids = idlist.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < ids.Length; i++)
            {
                StudentInfo s = new StudentInfo();
                s.ID = Convert.ToInt32(ids[i]);
                s.IsDelete = true;
                s.DeleteReason = deletereason;
                s.DeleteorGiveupStatic();
            }
        }
        //根据ID修改学生状态
        void UpdStatusByID()
        {
           // 获取修改ID
            string id = Request.Form["id"];
            string status = Request.Form["status"];
            StudentInfo s = new StudentInfo();
            s.ID = Convert.ToInt32(id);
            s.Status = Convert.ToInt32(status);
            s.MdfStatus();

        }
        //批量审核通过
        void UpdStatusList()
        {
            //获取修改ID
            string idlist = Request.Form["id"];
            string status = Request.Form["status"];
            string[] ids = idlist.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < ids.Length; i++)
            {
                StudentInfo s = new StudentInfo();
                s.ID = Convert.ToInt32(ids[i]);
                s.Status = Convert.ToInt32(status);
                s.MdfStatus();
            }
        }

        //根据ID修改学生状态为放弃
        void UpdStatusByGiveUpID()
        {
            //获取修改ID
            string id = Request.Form["id"];
            string status = Request.Form["status"];
            string updatereason = Request.Form["updatereason"];
            StudentInfo s = new StudentInfo();
            s.ID = Convert.ToInt32(id);
            s.Status = Convert.ToInt32(status);
            s.DeleteReason = updatereason;
            s.MdfGiveUpStatus();
        }
        //批量放弃
        void UpdStatusGiveUpList()
        {
            //获取修改ID
            string idlist = Request.Form["id"];
            string status = Request.Form["status"];
            string updatereason = Request.Form["updatereason"];
            string[] ids = idlist.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < ids.Length; i++)
            {
                StudentInfo s = new StudentInfo();
                s.ID = Convert.ToInt32(ids[i]);
                s.Status = Convert.ToInt32(status);
                s.DeleteReason = updatereason;
                s.MdfGiveUpStatus();
            }
        }
        //根据ID重置学生帐号密码
        void UpdStudentPswdReset()
        {
            string id = Request.Form["id"].ToString();
            string StudentPswd = Request.Form["StudentPswd"].ToString();
            StudentInfo s = new StudentInfo();
            s.MdfAccountPwd(Convert.ToInt64(id), BaseLibrary.ConvertToMD5(StudentPswd.ToLower()));
        }
        //void DirectoryInfoList()
        //{
        //    int day = 0;
        //    DirectoryInfo theFolder = new DirectoryInfo(Server.MapPath(@"..\StudentInfoToExcel\"));
        //    FileInfo[] dirInfo = theFolder.GetFiles();
        //    //遍历文件夹
        //    foreach (FileInfo NextFile in dirInfo)
        //    {
        //        string filename = NextFile.Name;
        //        string[] Date = filename.Split('_');
        //        if (Date.Length > 1)
        //        {
        //            string strTimeNow = DateTime.Now.ToString("yyyyMMdd");
        //            int d1 = int.Parse(Date[1]);
        //            int d2 = int.Parse(strTimeNow);
        //            int ND = d2 - d1;
        //            day = ND;
        //            if (day >= 1)
        //            {
        //                File.Delete(Server.MapPath(@"..\StudentInfoToExcel\" + filename));
        //            }
        //        }
        //    }
        //}
        //void derive_excelList()
        //{
        //    DirectoryInfoList();
        //    //获取修改ID
        //    string stuID = "";
        //    string idlist = Request.Form["id"];
        //    string[] ids = idlist.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        //    for (int i = 0; i < ids.Length; i++)
        //    {
        //        if (i != (ids.Length - 1))
        //        {
        //            stuID = stuID + ids[i] + " or id=";
        //        }
        //        else
        //        {
        //            stuID = stuID + ids[i];
        //        }
        //    }
        //    DataTable dt = DBAction.GetDTFromSQL("Select * From " + ETableName.Students + " where id=" + stuID);
        //    ExcelHelper excel = new ExcelHelper();
        //    string Toexcelpath = excel.SCexcel(dt, Server.MapPath(@"..\StudentInfoToExcel\"));
        //    Response.Clear();
        //    string result = "..\\" + "StudentInfoToExcel\\" + Toexcelpath;
        //    Response.Write(result);
        //    Response.End();
        //}



        //void derive_excelListAll()
        //{
        //    DirectoryInfoList();
        //    //获取修改ID
        //    string where = Request.Form["where"];
        //    string typeid = Request.Form["typeid"];
        //    ExcelHelper excel = new ExcelHelper();
        //    DataTable dt = excel.GetStudentBySchoolID(where, typeid);
        //    string Toexcelpath = excel.SCexcel(dt, Server.MapPath(@"..\StudentInfoToExcel\"));
        //    Response.Clear();
        //    string result = "..\\" + "StudentInfoToExcel\\" + Toexcelpath;
        //    Response.Write(result);
        //    Response.End();
        //}
    }
}